<?php

/* 店铺控制器 */
class StoreApp extends BackendApp
{
	var $_store_mod;

	function __construct()
	{
		$this->StoreApp();
	}

	function StoreApp()
	{
		parent::__construct();
		$this->_store_mod =& m('store');
	}

	function index()
	{
		$conditions = empty($_GET['wait_verify']) ? "state <> '" . STORE_APPLYING . "'" : "state = '" . STORE_APPLYING . "'";
		$filter = $this->_get_query_conditions(array(
		array(
                'field' => 'store_name',
                'equal' => 'like',
		),
		array(
                'field' => 'sgrade',
		),
		));
		$owner_name = trim($_GET['owner_name']);
		if ($owner_name)
		{

			$filter .= " AND (user_name LIKE '%{$owner_name}%' OR owner_name LIKE '%{$owner_name}%') ";
		}
		//更新排序
		if (isset($_GET['sort']) && isset($_GET['order']))
		{
			$sort  = strtolower(trim($_GET['sort']));
			$order = strtolower(trim($_GET['order']));
			if (!in_array($order,array('asc','desc')))
			{
				$sort  = 'sort_order';
				$order = '';
			}
		}
		else
		{
			$sort  = 'store_id';
			$order = 'desc';
		}

		$this->assign('filter', $filter);
		$conditions .= $filter;
		$page = $this->_get_page();
		$stores = $this->_store_mod->find(array(
            'conditions' => $conditions,
            'join'  => 'belongs_to_user',
            'fields'=> 'this.*,member.user_name',
            'limit' => $page['limit'],
            'count' => true,
            'order' => "$sort $order"
		));
		$sgrade_mod =& m('sgrade');
		$grades = $sgrade_mod->get_options();
		$this->assign('sgrades', $grades);

		$states = array(
		STORE_APPLYING  => LANG::get('wait_verify'),
		STORE_OPEN      => Lang::get('open'),
		STORE_CLOSED    => Lang::get('close'),
		);
		foreach ($stores as $key => $store)
		{
			$stores[$key]['sgrade'] = $grades[$store['sgrade']];
			$stores[$key]['state'] = $states[$store['state']];
			$stores[$key]['service'] = $states[$store['service']];
			$certs = empty($store['certification']) ? array() : explode(',', $store['certification']);
			for ($i = 0; $i < count($certs); $i++)
			{
				$certs[$i] = Lang::get($certs[$i]);
			}
			$stores[$key]['certification'] = join('<br />', $certs);
		}
		$this->assign('stores', $stores);

		$page['item_count'] = $this->_store_mod->getCount();
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->_format_page($page);
		$this->assign('filtered', $filter? 1 : 0); //是否有查询条件
		$this->assign('page_info', $page);

		$this->display('store.index.html');
	}
	function test()
	{
		if (!IS_POST)
		{
			$sgrade_mod =& m('sgrade');
			$grades = $sgrade_mod->find();
			if (!$grades)
			{
				$this->show_warning('set_grade_first');
				return;
			}
			$this->display('store.test.html');
		}
		else
		{
			$user_name = trim($_POST['user_name']);
			$password  = $_POST['password'];

			/* 连接到用户系统 */
			$ms =& ms();
			$user = $ms->user->get($user_name, true);
			if (empty($user))
			{
				$this->show_warning('user_not_exist');
				return;
			}
			if ($_POST['need_password'] && !$ms->user->auth($user_name, $password))
			{
				$this->show_warning('invalid_password');

				return;
			}

			$store = $this->_store_mod->get_info($user['user_id']);
			if ($store)
			{
				if ($store['state'] == STORE_APPLYING)
				{
					$this->show_warning('user_has_application');
					return;
				}
				else
				{
					$this->show_warning('user_has_store');
					return;
				}
			}
			else
			{
				header("Location:index.php?app=store&act=add&user_id=" . $user['user_id']);
			}
		}
	}

	function add()
	{
		$user_id = $_GET['user_id'];
		if (!$user_id)
		{
			$this->show_warning('Hacking Attempt');
			return;
		}

		if (!IS_POST)
		{
			/* 取得用户信息 */
			$user_mod =& m('member');
			$user = $user_mod->get_info($user_id);
			$this->assign('user', $user);

			$this->assign('store', array('state' => STORE_OPEN, 'recommended' => 0, 'sort_order' => 65535, 'end_time' => 0));

			$sgrade_mod =& m('sgrade');
			$this->assign('sgrades', $sgrade_mod->get_options());

			$this->assign('states', array(
			STORE_OPEN   => Lang::get('open'),
			STORE_CLOSED => Lang::get('close'),
			));

			$this->assign('services', array(
			STORE_OPEN   => Lang::get('open'),
			STORE_CLOSED => Lang::get('close'),
			));

			$officehours = array();
			for($i=0;$i<24*60;$i++){
				$officehours[$i] =  str_pad(intval($i/60),2,'0',STR_PAD_LEFT).':'.str_pad(($i%60),2,'0',STR_PAD_LEFT);
			}
			$this->assign('officehours', $officehours);

			$this->assign('recommended_options', array(
                '1' => Lang::get('yes'),
                '0' => Lang::get('no'),
			));

			$this->assign('scategories', $this->_get_scategory_options());

			$region_mod =& m('region');
			$this->assign('regions', $region_mod->get_options(0));

			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,mlselection.js'
                ));
                $this->assign('enabled_subdomain', ENABLED_SUBDOMAIN);
                $this->display('store.form.html');
		}
		else
		{
			/* 检查名称是否已存在 */
			if (!$this->_store_mod->unique(trim($_POST['store_name'])))
			{
				$this->show_warning('name_exist');
				return;
			}
			$domain = empty($_POST['domain']) ? '' : trim($_POST['domain']);
			if (!$this->_store_mod->check_domain($domain, Conf::get('subdomain_reserved'), Conf::get('subdomain_length')))
			{
				$this->show_warning($this->_store_mod->get_error());

				return;
			}
			$data = array(
                'store_id'     => $user_id,
                'store_name'   => $_POST['store_name'],
                'owner_name'   => $_POST['owner_name'],
                'owner_card'   => $_POST['owner_card'],
                'region_id'    => $_POST['region_id'],
                'region_name'  => $_POST['region_name'],
                'address'      => $_POST['address'],
                'zipcode'      => $_POST['zipcode'],
                'tel'          => $_POST['tel'],
                'sgrade'       => $_POST['sgrade'],
                'end_time'     => empty($_POST['end_time']) ? 0 : gmstr2time(trim($_POST['end_time'])),
                'state'        => $_POST['state'],
            	'service'	   => $_POST['service'],
            	'officestart'	   => $_POST['officestart'],
            	'officeend'	   => $_POST['officeend'],
                'recommended'  => $_POST['recommended'],
                'description'  => $_POST['description'],
                'sort_order'   => $_POST['sort_order'],
                'add_time'     => gmtime(),
                'domain'       => $domain,
			);
			$certs = array();
			isset($_POST['autonym']) && $certs[] = 'autonym';
			isset($_POST['material']) && $certs[] = 'material';
			$data['certification'] = join(',', $certs);
			$id=$this->_store_mod->add($data);
			if ($id === false)
			{
				$this->show_warning($this->_store_mod->get_error());
				return false;
			}

			$this->_store_mod->unlinkRelation('has_scategory', $user_id);
			$cate_id = intval($_POST['cate_id']);
			if ($cate_id > 0)
			{
				$this->_store_mod->createRelation('has_scategory', $user_id, $cate_id);
			}
			/* 处理上传logo */
			$logo = $this->_upload_logo($id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_store_mod->edit($id, array('store_logo' => $logo)); //将logo地址记下
			/* 处理上传横幅*/
			$banner = $this->_upload_banner($id);
			if ($banner === false)
			{
				return;
			}
			$banner && $this->_store_mod->edit($id, array('store_banner' => $banner)); //将banner地址记下

			$this->show_message('add_ok',
                'back_list',    'index.php?app=store',
                'continue_add', 'index.php?app=store&amp;act=test'
                );
		}
	}

	function edit()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST)
		{
			/* 是否存在 */
			$store = $this->_store_mod->get_info($id);
			if (!$store)
			{
				$this->show_warning('store_empty');
				return;
			}
			if ($store['certification'])
			{
				$certs = explode(',', $store['certification']);
				foreach ($certs as $cert)
				{
					$store['cert_' . $cert] = 1;
				}
			}
			$this->assign('store', $store);

			$sgrade_mod =& m('sgrade');
			$this->assign('sgrades', $sgrade_mod->get_options());

			$this->assign('states', array(
			STORE_OPEN   => Lang::get('open'),
			STORE_CLOSED => Lang::get('close'),
			));

			$this->assign('services', array(
			STORE_OPEN   => Lang::get('open'),
			STORE_CLOSED => Lang::get('close'),
			));

			$officehours = array();
			for($i=0;$i<24*60;$i++){
				$officehours[$i] =  str_pad(intval($i/60),2,'0',STR_PAD_LEFT).':'.str_pad(($i%60),2,'0',STR_PAD_LEFT);
			}
			$this->assign('officehours', $officehours);

			$this->assign('recommended_options', array(
                '1' => Lang::get('yes'),
                '0' => Lang::get('no'),
			));

			$region_mod =& m('region');
			$this->assign('regions', $region_mod->get_options(0));

			$this->assign('scategories', $this->_get_scategory_options());

			$scates = $this->_store_mod->getRelatedData('has_scategory', $id);
			$this->assign('scates', array_values($scates));

			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,mlselection.js'
                ));
                $this->assign('enabled_subdomain', ENABLED_SUBDOMAIN);
                $this->display('store.form.html');
		}
		else
		{
			/* 检查名称是否已存在 */
			if (!$this->_store_mod->unique(trim($_POST['store_name']), $id))
			{
				$this->show_warning('name_exist');
				return;
			}
			$store_info = $this->_store_mod->get_info($id);
			$domain = empty($_POST['domain']) ? '' : trim($_POST['domain']);
			if ($domain && $domain != $store_info['domain'])
			{
				if (!$this->_store_mod->check_domain($domain, Conf::get('subdomain_reserved'), Conf::get('subdomain_length')))
				{
					$this->show_warning($this->_store_mod->get_error());

					return;
				}
			}

			$data = array(
                'store_name'   => $_POST['store_name'],
                'owner_name'   => $_POST['owner_name'],
                'owner_card'   => $_POST['owner_card'],
                'region_id'    => $_POST['region_id'],
                'region_name'  => $_POST['region_name'],
                'address'      => $_POST['address'],
                'zipcode'      => $_POST['zipcode'],
                'tel'          => $_POST['tel'],
                'sgrade'       => $_POST['sgrade'],
                'end_time'     => empty($_POST['end_time']) ? 0 : gmstr2time(trim($_POST['end_time'])),
                'state'        => $_POST['state'],
            	'service'        => $_POST['service'],
            	'officestart'	   => $_POST['officestart'],
            	'officeend'	   => $_POST['officeend'],
                'sort_order'   => $_POST['sort_order'],
                'recommended'  => $_POST['recommended'],
                'description'  => $_POST['description'],
                'domain'       => $domain,
			);
			$data['state'] == STORE_CLOSED && $data['close_reason'] = $_POST['close_reason'];
			$data['service'] == STORE_CLOSED && $data['service_reason'] = $_POST['service_reason'];
			$certs = array();
			isset($_POST['autonym']) && $certs[] = 'autonym';
			isset($_POST['material']) && $certs[] = 'material';
			$data['certification'] = join(',', $certs);

			$old_info = $this->_store_mod->get_info($id); // 修改前的店铺信息
			$this->_store_mod->edit($id, $data);

			$this->_store_mod->unlinkRelation('has_scategory', $id);
			$cate_id = intval($_POST['cate_id']);
			if ($cate_id > 0)
			{
				$this->_store_mod->createRelation('has_scategory', $id, $cate_id);
			}

			/* 如果修改了店铺状态，通知店主 */
			if ($old_info['state'] != $data['state'])
			{
				$ms =& ms();
				if ($data['state'] == STORE_CLOSED)
				{
					// 关闭店铺
					$subject = Lang::get('close_store_notice');
					//$content = sprintf(Lang::get(), $data['close_reason']);
					$content = get_msg('toseller_store_closed_notify',array('reason' => $data['close_reason']));
				}
				else
				{
					// 开启店铺
					$subject = Lang::get('open_store_notice');
					$content = Lang::get('toseller_store_opened_notify');
				}
				$ms->pm->send(MSG_SYSTEM, $old_info['store_id'], '', $content);
				$this->_mailto($old_info['email'], $subject, $content);
			}
			/* 处理上传logo */
			$logo = $this->_upload_logo($id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_store_mod->edit($id, array('store_logo' => $logo)); //将logo地址记下
			/* 处理上传横幅*/
			$banner = $this->_upload_banner($id);
			if ($banner === false)
			{
				return;
			}
			$banner && $this->_store_mod->edit($id, array('store_banner' => $banner)); //将banner地址记下
				

			$ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
			$this->show_message('edit_ok',
                'back_list',    'index.php?app=store&page=' . $ret_page,
                'edit_again',   'index.php?app=store&amp;act=edit&amp;id=' . $id
			);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();
		if (in_array($column ,array('recommended','sort_order')))
		{
			$data[$column] = $value;
			$this->_store_mod->edit($id, $data);
			if(!$this->_store_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}

	function drop()
	{
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$id)
		{
			$this->show_warning('no_store_to_drop');
			return;
		}

		$ids = explode(',', $id);
		foreach ($ids as $id)
		{
			$this->_drop_store_image($id); // 注意这里要先删除图片，再删除店铺，因为删除图片时要查店铺信息
		}
		if (!$this->_store_mod->drop($ids))
		{
			$this->show_warning($this->_store_mod->get_error());
			return;
		}

		/* 通知店主 */
		$user_mod =& m('member');
		$users = $user_mod->find(array(
            'conditions' => "user_id" . db_create_in($ids),
            'fields'     => 'user_id, user_name, email',
		));
		foreach ($users as $user)
		{
			$ms =& ms();
			$subject = Lang::get('drop_store_notice');
			$content = get_msg('toseller_store_droped_notify');
			$ms->pm->send(MSG_SYSTEM, $user['user_id'], $subject, $content);
			$this->_mailto($user['email'], $subject, $content);
		}

		$this->show_message('drop_ok');
	}

	/* 更新排序 */
	function update_order()
	{
		if (empty($_GET['id']))
		{
			$this->show_warning('Hacking Attempt');
			return;
		}

		$ids = explode(',', $_GET['id']);
		$sort_orders = explode(',', $_GET['sort_order']);
		foreach ($ids as $key => $id)
		{
			$this->_store_mod->edit($id, array('sort_order' => $sort_orders[$key]));
		}

		$this->show_message('update_order_ok');
	}

	/* 查看并处理店铺申请 */
	function view()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST)
		{
			/* 是否存在 */
			$store = $this->_store_mod->get_info($id);
			if (!$store)
			{
				$this->show_warning('Hacking Attempt');
				return;
			}

			$sgrade_mod =& m('sgrade');
			$sgrades = $sgrade_mod->get_options();
			$store['sgrade'] = $sgrades[$store['sgrade']];
			$this->assign('store', $store);

			$scates = $this->_store_mod->getRelatedData('has_scategory', $id);
			$this->assign('scates', $scates);

			$this->display('store.view.html');
		}
		else
		{
			$ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
			/* 批准 */
			if (isset($_POST['agree']))
			{
				$this->_store_mod->edit($id, array(
                    'state'      => STORE_OPEN,
                    'add_time'   => gmtime(),
                    'sort_order' => 65535,
				));

				$content = get_msg('toseller_store_passed_notify');
				$ms =& ms();
				$ms->pm->send(MSG_SYSTEM, $id, '', $content);
				$store_info = $this->_store_mod->get_info($id);
				$this->send_feed('store_created', array(
                    'user_id'   =>  $store_info['store_id'],
                    'user_name'   => $store_info['user_name'],
                    'store_url'   => SITE_URL . '/' . url('app=store&id=' . $store_info['store_id']),
                    'seller_name'   => $store_info['store_name'],
				));
				$this->_hook('after_opening', array('user_id' => $id));
				$this->show_message('agree_ok',
                    'edit_the_store', 'index.php?app=store&amp;act=edit&amp;id=' . $id,
                    'back_list', 'index.php?app=store&wait_verify=1&page=' . $ret_page
				);
			}
			/* 拒绝 */
			elseif (isset($_POST['reject']))
			{
				$reject_reason = trim($_POST['reject_reason']);
				if (!$reject_reason)
				{
					$this->show_warning('input_reason');
					return;
				}

				$content = get_msg('toseller_store_refused_notify', array('reason' => $reject_reason));
				$ms =& ms();
				$ms->pm->send(MSG_SYSTEM, $id, '', $content);

				$this->_drop_store_image($id); // 注意这里要先删除图片，再删除店铺，因为删除图片时要查店铺信息
				$this->_store_mod->drop($id);
				$this->show_message('reject_ok',
                    'back_list', 'index.php?app=store&wait_verify=1&page=' . $ret_page
				);
			}
			else
			{
				$this->show_warning('Hacking Attempt');
				return;
			}
		}
	}

	function batch_edit()
	{
		if (!IS_POST)
		{
			$sgrade_mod =& m('sgrade');
			$this->assign('sgrades', $sgrade_mod->get_options());

			$region_mod =& m('region');
			$this->assign('regions', $region_mod->get_options(0));

			$this->headtag('<script type="text/javascript" src="{lib file=mlselection.js}"></script>');
			$this->display('store.batch.html');
		}
		else
		{
			$id = isset($_POST['id']) ? trim($_POST['id']) : '';
			if (!$id)
			{
				$this->show_warning('Hacking Attempt');
				return;
			}

			$ids = explode(',', $id);
			$data = array();
			if ($_POST['region_id'] > 0)
			{
				$data['region_id'] = $_POST['region_id'];
				$data['region_name'] = $_POST['region_name'];
			}
			if ($_POST['sgrade'] > 0)
			{
				$data['sgrade'] = $_POST['sgrade'];
			}
			if ($_POST['certification'])
			{
				$certs = array();
				if ($_POST['autonym'])
				{
					$certs[] = 'autonym';
				}
				if ($_POST['material'])
				{
					$certs[] = 'material';
				}
				$data['certification'] = join(',', $certs);
			}
			if ($_POST['recommended'] > -1)
			{
				$data['recommended'] = $_POST['recommended'];
			}
			if (trim($_POST['sort_order']))
			{
				$data['sort_order'] = intval(trim($_POST['sort_order']));
			}

			if (empty($data))
			{
				$this->show_warning('no_change_set');
				return;
			}

			$this->_store_mod->edit($ids, $data);
			$ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
			$this->show_message('edit_ok',
                'back_list', 'index.php?app=store&page=' . $ret_page);
		}
	}

	function check_name()
	{
		$id         = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$store_name = empty($_GET['store_name']) ? '' : trim($_GET['store_name']);

		if (!$this->_store_mod->unique($store_name, $id))
		{
			echo ecm_json_encode(false);
			return;
		}
		echo ecm_json_encode(true);
	}

	/* 删除店铺相关图片 */
	function _drop_store_image($store_id)
	{
		$files = array();

		/* 申请店铺时上传的图片 */
		$store = $this->_store_mod->get_info($store_id);
		for ($i = 1; $i <= 3; $i++)
		{
			if ($store['image_' . $i])
			{
				$files[] = $store['image_' . $i];
			}
		}

		/* 店铺设置中的图片 */
		if ($store['store_banner'])
		{
			$files[] = $store['store_banner'];
		}
		if ($store['store_logo'])
		{
			$files[] = $store['store_logo'];
		}

		/* 删除 */
		foreach ($files as $file)
		{
			$filename = ROOT_PATH . '/' . $file;
			if (file_exists($filename))
			{
				@unlink($filename);
			}
		}
	}

	/* 取得店铺分类 */
	function _get_scategory_options()
	{
		$mod =& m('scategory');
		$scategories = $mod->get_list();
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($scategories, 'cate_id', 'parent_id', 'cate_name');

		return $tree->getOptions();
	}
	//logo上传
	function _upload_logo($id)
	{
		$file = $_FILES['store_logo'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['store_logo']);//上传

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=store&amp;act=edit&amp;id=' . $id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/store', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
	//横幅上传
	function _upload_banner($id)
	{
		$file = $_FILES['store_banner'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['store_banner']);//上传

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=store&amp;act=edit&amp;id=' . $id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/store', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
}